﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using gzdanny.web_lib;
using gzdanny.pub_lib;
using System.Text.RegularExpressions;
using Excel = Microsoft.Office.Interop.Excel;


namespace Web采集工具
{
    public partial class Main : Form
    {
        WebGrabber myGrabber = new WebGrabber("UTF-8");
        MatchCollection myMatchs;

        public Main()
        {
            InitializeComponent();
            tCtrl_Main.SelectedTab = tPage_Words;
        }

        #region 页面采集
        private void btn_OpenURL_Click(object sender, EventArgs e)
        {
            myGrabber.RegExp = tBox_RegExp.Text;
            if (myGrabber.Open(tBox_URL.Text))
            {
                tBox_HtmlDOC.Clear();
                if (myGrabber.RegExp.Length > 5)
                {
                    myMatchs = myGrabber.Matchs;
                    foreach (Match rec in myMatchs)
                    {
                        foreach (Group str in rec.Groups)
                        {
                            tBox_HtmlDOC.AppendText(str.Value + "\r\n…………\r\n");
                        }
                        tBox_HtmlDOC.AppendText("\r\n\r\n____________\r\n\r\n");
                    }
                }
                tBox_HtmlDOC.AppendText(myGrabber.HtmlDoc);
            }
            else
            {
                tools.show_err("网址打开失败！");
            }
        }

        private void tBox_URL_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return) { btn_OpenURL.PerformClick(); }
        }

        private void tBox_HtmlDOC_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.A) { tBox_HtmlDOC.SelectAll(); }
        }
        #endregion

        #region 有道词典-单词
        private void btn_GrabWord_Click(object sender, EventArgs e)
        {
            myGrabber.URL = @"http://dict.youdao.com/search?q=" + tBox_Word.Text;
            myGrabber.RegExp = @"<!-- 结果内容 -->[\S\s]+?<span class=""keyword"">(?<word>.+)</span>(?:[\S\s]+<span class=""phonetic"">(?<pronunciation>.+)</span>)?[\S\s]+?<div class=""trans-container"">[\s]+<ul>[\s]+<li>(?<meaning>[\S\s]+?)</li>[\s]+</ul>(?:[\s]+<p class=""additional"">\[(?<change>[\S\s]+?)]</p>)?[\S\s]+?<div id=""bilingual"" class=""trans-container  tab-content"">[\s]+<ul class=""ol"">(?<bilingual>[\S\s]+?)</ul>";
            if (myGrabber.Open())
            {
                Match rec = myGrabber.Matchs[0];
                tBox_WordData.Clear();
                tBox_WordData.AppendText("\r\n单词：" + rec.Groups["word"]);
                tBox_WordData.AppendText("\r\n音标：" + rec.Groups["pronunciation"]);

                tBox_WordData.AppendText("\r\n中文：" + Regex.Replace(rec.Groups["meaning"].Value, @"</li>[\s]+?<li>", "  "));
                tBox_WordData.AppendText("\r\n词态：" + Regex.Replace(rec.Groups["change"].Value, @"[\s]+", ""));
                tBox_WordData.AppendText("\r\n例句：\r\n" + clsBilingual(rec.Groups["bilingual"].Value));
            }
        }

        private string clsBilingual(string orgData)
        {
            StringBuilder result = new StringBuilder();
            Regex rx = new Regex(@"<li>\s*<p>(?<EnSent>.+?)\s*<a class=""sp .+/a>\s*</p>\s*<p>(?<CnSent>.+?)\s*</p>[\S\s]+?</li>");
            Regex clsHTML = new Regex(@"<.+?>");
            MatchCollection mcs = rx.Matches(orgData);
            foreach (Match mc in mcs)
            {
                result.AppendLine(clsHTML.Replace(mc.Groups["EnSent"].Value, "") + "*");
                result.AppendLine(clsHTML.Replace(mc.Groups["CnSent"].Value, "") + "+");
            }
            return result.ToString();
        }

        private void tBox_Word_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return) { btn_GrabWord.PerformClick(); }
        }
        private void tBox_WordData_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.A) { tBox_WordData.SelectAll(); }
        }
        #endregion

        #region 采集单词列表
        private void btn_OpenWordFile_Click(object sender, EventArgs e)
        {
            if (oFD_WordListFile.ShowDialog() == DialogResult.OK)
            {
                tBox_WordListFilePath.Text = oFD_WordListFile.FileName;
            }
        }
        private void tPage_Words_DragDrop(object sender, DragEventArgs e)
        {
            String tmp = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0) as String;
            if (tmp != null)
            {
                tBox_WordListFilePath.Text = tmp;
            }
        }
        private void tPage_Words_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.None;
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
        }


        private void btn_GrabList_Click(object sender, EventArgs e)
        {
            btn_GrabList.Enabled = false;
            btn_OpenWordFile.Enabled = false;
            tBox_WordListFilePath.Enabled = false;
            tPage_Words.AllowDrop = false;
            cBox_CleanMode.Enabled = false;

            WebGrabber myGrabber = new WebGrabber("UTF-8");
            string[] words;
            try
            {
                words = System.IO.File.ReadAllLines(tBox_WordListFilePath.Text, Encoding.UTF8);
            }
            catch
            {
                tools.show_err("单词列表错误，请提供UTF-8格式的文本文件。");
                return;
            }

            tools.pBar_Prepare(pBar_WordList, 0, words.Length);
            tBox_WordListLog.Clear();
            string[,] xlsData = new string[words.Length, 5];
            int idx = 0;
            myGrabber.RegExp = @"<!-- 结果内容 -->[\S\s]+?<span class=""keyword"">(?<word>.+)</span>(?:[\S\s]+<span class=""phonetic"">(?<pronunciation>.+)</span>)?[\S\s]+?<div class=""trans-container"">[\s]+<ul>[\s]+<li>(?<meaning>[\S\s]+?)</li>[\s]+</ul>(?:[\s]+<p class=""additional"">\[(?<change>[\S\s]+?)]</p>)?[\S\s]+?<div id=""bilingual"" class=""trans-container  tab-content"">[\s]+<ul class=""ol"">(?<bilingual>[\S\s]+?)</ul>";
            foreach (string word in words)
            {
                pBar_WordList.PerformStep();
                try
                {
                    if (word.Trim().Length > 0 && myGrabber.Open(@"http://dict.youdao.com/search?q=" + word.Trim()))
                    {
                        Match rec = myGrabber.Matchs[0];
                        if (rec.Groups["word"].Value.ToLower() == word.Trim().ToLower())
                        {
                            //List<string> thisWord = new List<string>();
                            //thisWord.Add(rec.Groups["word"].Value);
                            //thisWord.Add(rec.Groups["pronunciation"].Value);
                            //thisWord.Add(Regex.Replace(rec.Groups["meaning"].Value, @"</li>[\s]+?<li>", "  "));
                            //thisWord.Add(Regex.Replace(rec.Groups["change"].Value, @"[\s]+", ""));
                            //thisWord.Add(clsBilingual(rec.Groups["bilingual"].Value));
                            xlsData[idx, 0] = rec.Groups["word"].Value;
                            xlsData[idx, 1] = rec.Groups["pronunciation"].Value;
                            xlsData[idx, 2] = Regex.Replace(rec.Groups["meaning"].Value, @"</li>[\s]+?<li>", "  ");
                            xlsData[idx, 3] = Regex.Replace(rec.Groups["change"].Value, @"[\s]+", "");
                            xlsData[idx, 4] = clsBilingual(rec.Groups["bilingual"].Value);

                            tBox_WordListLog.AppendText("\r\n单词：" + xlsData[idx, 0]);
                            tBox_WordListLog.AppendText("\r\n音标：" + xlsData[idx, 1]);
                            tBox_WordListLog.AppendText("\r\n中文：" + xlsData[idx, 2]);
                            tBox_WordListLog.AppendText("\r\n词态：" + xlsData[idx, 3]);
                            tBox_WordListLog.AppendText("\r\n例句：\r\n" + xlsData[idx, 4]);
                            tBox_WordListLog.AppendText("\r\n------------------\r\n");

                            idx++;
                        }
                        //非清洁模式，将拼写不相符的单词也记录下来
                        else if (!cBox_CleanMode.Checked)
                        {
                            xlsData[idx, 0] = "[E]" + rec.Groups["word"].Value;
                            xlsData[idx, 1] = rec.Groups["pronunciation"].Value;
                            xlsData[idx, 2] = Regex.Replace(rec.Groups["meaning"].Value, @"</li>[\s]+?<li>", "  ");
                            xlsData[idx, 3] = Regex.Replace(rec.Groups["change"].Value, @"[\s]+", "");
                            xlsData[idx, 4] = clsBilingual(rec.Groups["bilingual"].Value);

                            tBox_WordListLog.AppendText("\r\n可能单词：" + xlsData[idx, 0]);
                            tBox_WordListLog.AppendText("\r\n音标：" + xlsData[idx, 1]);
                            tBox_WordListLog.AppendText("\r\n中文：" + xlsData[idx, 2]);
                            tBox_WordListLog.AppendText("\r\n词态：" + xlsData[idx, 3]);
                            tBox_WordListLog.AppendText("\r\n例句：\r\n" + xlsData[idx, 4]);
                            tBox_WordListLog.AppendText("\r\n------------------\r\n");

                            idx++;
                        }
                    }
                }
                catch
                {

                    if (!cBox_CleanMode.Checked)
                    {
                        xlsData[idx, 0] = "[E]" + word.Trim();

                        tBox_WordListLog.AppendText("\r\n错误单词：" + xlsData[idx, 0]);
                        tBox_WordListLog.AppendText("\r\n------------------\r\n");

                        idx++;
                    }
                }

                Application.DoEvents();
            }

            if (idx > 0)
            {
                Excel.Application mExcel = new Excel.Application();
                Excel._Worksheet mSheet = (Excel._Worksheet)mExcel.Workbooks.Add(true).Worksheets.get_Item(1);
                Excel.Range mRange = mSheet.get_Range("A1", "E" + idx.ToString());
                mRange.Value2 = xlsData;
                //mExcel.ActiveWorkbook.SaveAs(tBox_WordListFilePath.Text + ".xls", Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlUserResolution, false, null, null, null);
                mExcel.ActiveWorkbook.Close(true, tBox_WordListFilePath.Text + ".xls", true);
                mExcel.Quit();

                tools.show_msg("成功采集单词：" + idx.ToString() + "个");
            }
            else
            {
                tools.show_err("采集不到单词！");
            }

            btn_GrabList.Enabled = true;
            btn_OpenWordFile.Enabled = true;
            tBox_WordListFilePath.Enabled = true;
            tPage_Words.AllowDrop = true;
            cBox_CleanMode.Enabled = true;

        }
        #endregion
    }
}